<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>_包装(预先打单)</title>
<script type='text/javascript'
      src='http://wms.ttyuncang.com/WebSpeech.js'></script>
<script type='text/javascript'
      src='${ctx}/static/js/map.js'></script>
<script type="text/javascript">
  $(function() {
	  $("#txt_expressNo").focus();
	 // WebSpeech.server = 'http://120.24.87.124/cgi-bin/ekho2.pl';
	 // WebSpeech.speak('成功');
	 
	  //add bug:447  20160804
	  $("#lock_num_f").iCheck({checkboxClass: 'icheckbox_minimal'});
	  $('#lock_num_f').on('ifChecked', function (event) {
	       $("#txt_sku_num").attr("readOnly", true);
	  });
	  $('#lock_num_f').on('ifUnchecked', function (event) {
	 	  $("#txt_sku_num").removeAttr("readOnly");
	  });
  });
  WebSpeech.ready(function() {
	  WebSpeech.speak('听到声音了吗？');
  });
  var curOrderno;
  var skuMap=new Map();
  var alSkuMap=new Map();
  var curinput;
  var curflag=0;
  function reset(){
	  $("#txt_info").html('');
	  $("#txt_sku").focus();
  }
  
  function keydown(){
	  curOrderno='';
	  $("#msg_sku").html("");
	  $("#msg_pkg").html("");
	  $("#btn_pkg").hide();
	  $("[name='exist']").hide();
      $("[name='noexist']").show();
  }
  
  
  //验证
  function change_sku(){
	  //add bug:447  20160804
	  var num = $("#txt_sku_num").val().trim();
	  if (num.length==0) {
		  WebSpeech.speak("数量错误");
		  $("#msg_sku").html("数量错误1");
		  return;
	  }
	  var reg = /^(0|[1-9]\d*)$/;
	  if (reg.test(String(num))) {
		  num = parseInt(num);
	  } else {
		  WebSpeech.speak("数量错误");
		  $("#msg_sku").html("数量错误2");
		  return;
	  }
  	
	  var sku=$("#txt_sku").val().trim();
	  if(sku==''){
		  $("#msg_sku").select();
		  $("#msg_sku").focus();
		  $("#msg_sku").html("请录入SKU编号!");
	  }
	  
	  if(skuMap.get(sku) == null || skuMap.get(sku) ==0){
		  WebSpeech.speak("错误");
		  $("#msg_sku").html("错误");
		  $("#txt_sku").select();
		  $("#txt_sku").focus();
		  return;
	  }else{
		 var qty = skuMap.get(sku); 
		 //add bug:447  20160804
		 if (num - qty >  0) {
			  $("#msg_sku").html("错误:超出待验货数量");
			  WebSpeech.speak("错误, 超出待验货数量");
			  return;
		 }
		 
		 skuMap.remove(sku);
		 skuMap.put(sku,qty-num);
		 
		 if(skuMap.get(sku) == 0){
			 skuMap.remove(sku);
		 }
		 
		 $("#txt_sku").val("");
		 $("#txt_sku").focus();
		 WebSpeech.speak("正确");
      }
	  var info=$("#txt_info").html();
	  if (info.length > 0) {
		  info+=";";
	  }
	  
	 //add bug:447  20160804
	  var num2 = num;
	  while (num2-- > 0) {
		  if (num2 == 0) {
			  info+= sku;
		  } else {
			  info+=sku + ";";
		  }
		  $("#txt_info").html(info);
	  }
	  
	  var i = 0;
	  var flag=0;
	  if(!skuMap.isEmpty()){
		  $("#orderItems")
			.find("tr")
			.each(
					function(j) {
						var tdArr = $(this).children();
						if (tdArr.eq(1).text() == sku) {
							flag=j;
							if(parseInt(tdArr.eq(4).text())==0){
								i=-1;
							}else if ((parseInt(tdArr.eq(4).text())+parseInt(tdArr.eq(5).text()))> parseInt(tdArr.eq(3).text())) {
								i = -1;
							} else if((parseInt(tdArr.eq(4).text())+parseInt(tdArr.eq(5).text()))== parseInt(tdArr.eq(3).text())){
								i = 1;
								tdArr.eq(4).html(parseInt(tdArr.eq(4).text())-num);
								tdArr.eq(5).html(parseInt(tdArr.eq(5).text())+num);
							} else{
								i = 1;
								tdArr.eq(4).html(parseInt(tdArr.eq(4).text())-num);
								tdArr.eq(5).html(parseInt(tdArr.eq(5).text())+num);
							}

						}
					});
		 // $("#orderItems tr:eq("+flag+")").insertBefore( $("#orderItems tr:eq(0)"));
		/*   if (i == 0) {
				$("#msg_sku").html("该SKU不在订单明细中");
				$("#msg_sku").css("color", "red");
				WebSpeech.speak("该SKU不在订单明细中");
				return;
			} else if (i == 1) {
				$("#msg_sku").css("color", "green");
				$("#msg_sku").html("成功");
				WebSpeech.speak("成功");
			} else if (i == -1) {
				$("#msg_sku").css("color", "red");
				$("#msg_sku").html(sku + "超量了");
				WebSpeech.speak(sku + "超量了");
			} */
		 if(curflag==flag){
			 $("#orderItems tr:eq("+curflag+")").css("background","green");
		  }else{
			$("#orderItems tr:eq("+flag+")").css("background","green");
			$("#orderItems tr:eq("+curflag+")").removeAttr("style");
			curflag=flag;
		  } 
		  return;
	  }
	  $.ajax({
			type:'post',
			url:'${ctx}/pkgTask/ajaxCheckGoodsInfo',
			data:{
				orderno:curOrderno,
				skuInfo:info
			},
			dataType:'json',
			success:function(data) {
				var msg=data.msg;
				if(msg=='success'){
					pkg();
				}
				else{
					$("#txt_sku").val('');
					$("#txt_sku").focus();
					$("#msg_pkg").html("");
					WebSpeech.speak("失败");
				}
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
	  
	  
	  
  }
  
  function showOrderInfo(workOrder){
       $("[name='exist']").show();
       $("[name='noexist']").hide();
       
       $("#orderno").html(workOrder.order.ordeno);
       $("#expressNo").html(workOrder.expressNo);
       $("#expressName").html(workOrder.order.cacode);
       $("#address").html(workOrder.order.addres);
       $("#area").html(workOrder.order.provna+"/"+workOrder.order.cityna+"/"+workOrder.order.distna);
       $("#postco").html(workOrder.order.postco);
       $("#custna").html(workOrder.order.recena);
       $("#mobile").html(workOrder.order.mobile);
       $("#teleph").html(workOrder.order.teleph);
       
       var orderType=workOrder.order.orderType;
       if(orderType=='single'){ 
    	   $("#orderType").html("单件");
       }else if(orderType=='multiple'){
    	   $("#orderType").html("多件");
       }
       
       var billType=workOrder.billType;
       if(billType=='cbill'){ 
    	   $("#billType").html("普通运单");
    	   
       }else if(billType=='ebill'){
    	   $("#billType").html("电子运单");
       }
       
       var flowType=workOrder.flowType;
       if(flowType=='prePrint'){ 
    	   $("#flowType").html("预先打单");
       }else if(flowType=='intimePrint'){
    	   $("#flowType").html("及时打单");
       }
       
       var status=workOrder.status;
       if(status=='1'){ 
    	   $("#status").html("创建批次");
       }else if(status=='2'){
    	   $("#status").html("创建任务");
       }else if(status=='3'){
    	   $("#status").html("已拣货");
       }else if(status=='4'){
    	   $("#status").html("已灯光拣选");
       }else if(status=='5'){
    	   $("#status").html("已包装");
       }else if(status=='6'){
    	   $("#status").html("已发货");
       }else if(status=='7'){
    	   $("#status").html("已签收");
       }else if(status=='8'){
    	   $("#status").html("异常");
       }else if(status=='9'){
    	   $("#status").html("待包装");
       }
       
       //订单明细
       var str='';
       skuMap=new Map();
       var skuNameMap=new Map();
       var m=0;
       $("#orderItems").html("");
       $.each(workOrder.order.orderItems,function(i,orderItem){
   		    var flag=0;
   		    str='';
    	    if(skuMap.get(orderItem.sku)==null){
         	  skuMap.put(orderItem.sku,orderItem.qty);
           }else {
         	  skuMap.put(orderItem.sku,skuMap.get(orderItem.sku)+orderItem.qty);
           }
    	    $("#orderItems")
			.find("tr")
			.each(
					function(j) {
						var tdArr = $(this).children();
						if (tdArr.eq(1).text() == orderItem.sku) {
							tdArr.eq(3).html(skuMap.get(orderItem.sku));
							tdArr.eq(4).html(skuMap.get(orderItem.sku));
							//begin add by zhangweize 页面显示内容重复
							flag=0;
							return false;
							//end add by zhangweize 页面显示内容重复
						}else{
							flag=1;
							
						}
			});
    	    if( $("#orderItems").find("tr").size()==0){
    	    	m++;
    	    	str+="<tr>";
                str+="<td>"+m+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+orderItem.sku+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+orderItem.ediGoodsSku.skuName+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+skuMap.get(orderItem.sku)+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+skuMap.get(orderItem.sku)+"</td>"; 
                str+="<td style=\"vertical-align:middle; text-align:center;\">0</td>"; 
                str+="</tr>";  
                $("#orderItems").html(str);
    	    }else if(flag==1){
    	    	m++;
    	    	str+="<tr>";
                str+="<td>"+m+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+orderItem.sku+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+orderItem.ediGoodsSku.skuName+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+skuMap.get(orderItem.sku)+"</td>";
                str+="<td style=\"vertical-align:middle; text-align:center;\">"+skuMap.get(orderItem.sku)+"</td>"; 
                str+="<td style=\"vertical-align:middle; text-align:center;\">0</td>"; 
                str+="</tr>"; 
                var orderItemsHtml=$("#orderItems").html()+str;  
                $("#orderItems").html(orderItemsHtml);
    	    }

       });
      
  }
  
  //包装
  function pkg(){
      
     var btn=$("#btn_pkg");
     btn.attr("disabled",true);
     btn.attr("class","btn");
     
	  $.ajax({
			type:'post',
			url:'${ctx}/pkgTask/ajaxPkg',
			data:{
				orderno:curOrderno
			},
			dataType:'json',
			success:function(data) {
				var msg=data.msg;
				if(msg=='success'){ 
					$("#txt_orderno").val('');
					$("#txt_expressNo").val('');
					$("#txt_sku").val('');
					$("#txt_info").html('');
					if(curinput == 1){
						$("#txt_expressNo").focus();
					}else{
						$("#txt_orderno").focus();
					}
					$("[name='exist']").hide();
				    $("[name='noexist']").show();
				    $("#msg_pkg").css("color","green");
					$("#msg_pkg").html("订单("+curOrderno+")包装完成.");
					$("#msg_sku").html("");
					curOrderno='';
					WebSpeech.speak('完成');
					 $("#orderItems").html("");
				}else if(msg=='unprinted'){
				    $("#txt_orderno").val('');
					$("#txt_expressNo").val('');
					$("#txt_sku").val('');
					$("#txt_info").html('');
					$("#txt_orderno").focus();
					$("[name='exist']").hide();
				    $("[name='noexist']").show();
				    $("#msg_pkg").css("color","red");
					$("#msg_pkg").html("此订单("+curOrderno+"),未打印...");
					$("#msg_sku").html("");
					WebSpeech.speak('未打印');
					 $("#orderItems").html("");
				}
				else{
					alert("网络繁忙，请稍后重试...");
				}
				
				btn.attr("disabled",false);
				btn.css("class","btn-primary");
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
				btn.attr("disabled",false);
				btn.css("class","btn-primary");
			}
	   });
  }
  
  function change_packForm(packformStyles){
	 var tempStr = "<option value=''></option>";

	 $.each(packformStyles,function(i,obj){
		 tempStr+="<option value='"+obj.id+"'>"+obj.styleName+"</option>";      
		});
	 $("#packformStylesId").html(tempStr);
	 $("#packformStylesId").trigger("chosen:updated");
  }
  
  function change_orderno(){
	  $("#txt_expressNo").select();
	  $("#txt_expressNo").focus();
  }
  
  function change_expressNo(){
	  var orderno = $("#txt_orderno").val().trim();
	  var expressNo = $("#txt_expressNo").val().trim();
	  /* if(expressNo==''){
		   $("#msg_sku").css("color","red");
		   $("#msg_sku").html("请扫描运单编号...");
		   $("#txt_expressNo").focus();
	  } */
	  if(orderno.length>0){
			curinput = 2;//订单输入
		}else{
			curinput = 1;//运单输入
		}
	  $.ajax({
			type:'post',
			url:'${ctx}/pkgTask/ajaxCheckWorkOrder_2',
			data:{
				orderno:orderno,
				expressNo:expressNo
			},
			dataType:'json',
			success:function(data) {
				var msg=data.msg;
				var packformStyles=data.packformStyles;
				if(msg=='success'){
					if(data.workOrder.ebillNo.length<=0&&data.workOrder.expressNo.length<=0){
						alert("运单号为空不能进行包装操作！") ; 
						$("#txt_orderno").focus() ;
						$("#txt_orderno").val("") ;
						return ; 
					}
					$("#msg_sku").css("color","green");
					$("#msg_sku").html("请扫描商品SKU...");
					$("#txt_sku").focus();
					curflag=0;
					//显示订单信息
					var workOrder=data.workOrder;
				    curOrderno=workOrder.order.ordeno;
				    if(workOrder.ebillNo == ""){
				    	$("#txt_expressNo").val(workOrder.expressNo) ;
				    }else{
				    	workOrder.expressNo = workOrder.ebillNo ;
				    	$("#txt_expressNo").val(workOrder.ebillNo) ;
				    }
				    showOrderInfo(workOrder);
				    $("#txt_orderno").val(curOrderno);
				    change_packForm(packformStyles);
				    WebSpeech.speak('成功');
				}
				else if(msg=='cancel'){
				
				    $("#msg_sku").css("color","red");
					$("#msg_sku").html("此单是取消单，请处理！");
					WebSpeech.speak('已取消');
					//显示订单信息
					var workOrder=data.workOrder;
					if(workOrder.ebillNo != ""){
						workOrder.expressNo = workOrder.ebillNo ;
				    }
				    showOrderInfo(workOrder);
				    $("#txt_orderno").val('');
					$("#txt_expressNo").val('');
					if(orderno.length>0){
						$("#txt_orderno").focus();
					}else{
						$("#txt_expressNo").focus();
					}
					change_packForm(packformStyles);
					
				}
				else if(msg=='error'){

					$("#txt_sku").select();
					$("#txt_sku").focus();
					$("#msg_sku").css("color","red");
					$("#msg_sku").html("订单异常，请检查...");
					
					$("#txt_orderno").val('');
					$("#txt_expressNo").val('');
					if(orderno.length>0){
						$("#txt_orderno").focus();
					}else{
						$("#txt_expressNo").focus();
					}
					WebSpeech.speak('异常');
				}
				else{
					showWorkOrderStatus(orderno,msg);
				}
				
				
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   });
	  
  }
  function showWorkOrderStatus(orderno,msg){
    $("#txt_sku").select();
	$("#txt_sku").focus();
	$("#msg_sku").css("color","red");
	$("#msg_sku").html(msg+"...");
	
	$("#txt_orderno").val('');
	$("#txt_expressNo").val('');
	if(orderno.length>0){
		$("#txt_orderno").focus();
	}else{
		$("#txt_expressNo").focus();
	}
	WebSpeech.speak(msg);
  }
 
  
</script>
</head>

<body>
<section class="content">
 <div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<h3 class="box-title">扫描包装(预先打单)</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse" type="button">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
	<div class="box-body">
    <!-- 扫描包装(预先打单) -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		 <tr>
		    <td style="width: 100px; font-weight: bold; vertical-align: middle;text-align: center;">扫描单据：</td>
		    <td colspan="9">
		    <div class="col-sm-3">
				<div class="input-textgroup col-sm-12">
			      <span class="input-group-text">订单编号:</span>
			      <input type="text"  class="form-control" id="txt_orderno" onchange="change_expressNo();" onkeydown="keydown()" />
		      	</div>
		     </div>
		      <div class="col-sm-3">
				<div class="input-textgroup col-sm-12">
			      <span class="input-group-text">运单编号:</span>
			      <input type="text" class="form-control"  id="txt_expressNo" onchange="change_expressNo();" />
		     </div>
		     </div>
		      <span style="margin-left:12px; color: red;font-weight: bold;" id="msg_sku"></span>
		    </td>
		</tr>
		
	    <tr>
		    <td style="width: 100px; font-weight: bold; vertical-align: middle;text-align: center;">扫描商品：</td>
		    <td colspan="9">
		     <div class="col-sm-3">
				<div class="input-textgroup col-sm-12">
			      <span class="input-group-text">SKU编码:</span>
			      <input type="text" class="form-control" id="txt_sku" onchange="change_sku();" />
		     	</div>
		     </div>
		    <!-- begin bug:447 add 20160804 可按数量复核 -->
		     <div class="col-sm-2">
				<div class="input-textgroup col-sm-12">
			      <span class="input-group-text">数量:</span>
			      <input type="number" class="form-control col-sm-3" id="txt_sku_num" value="1"  style="width:100%" readonly='readonly'"/>
		     	</div>
		     </div>
		     <div class="col-sm-1">
				<div class="input-textgroup col-sm-12" id="lockchecheck">
			      <span class="input-group-text">锁定数量:</span>
			      <input id="lock_num_f" name="lock_num_f" type="checkbox" checked="checked" />
		     	</div>
		     </div>
		    <!-- end bug:447 add 20160804 可按数量复核 -->
		     <div class="col-sm-3">
				<div class="input-textgroup col-sm-12">
		   		   <span class="input-group-text" style="color: red;font-weight: bold;" id="msg_pkg"></span>
		   		</div>
		   	</div>
		    </td>
		</tr>
		
		 <tr>
		    <td style="width: 100px; font-weight: bold; vertical-align: middle;text-align: center;">扫描信息：</td>
		    <td colspan="9">
		      <textarea class="form-control" id="txt_info" style="margin-top: 10px; margin-left: 10px; width: 800px;" readonly="readonly"></textarea>
		      <input type="button" value="清空"  class="btn btn-info" onclick="reset();" style="margin-top: 7px; margin-left: 10px;" />
		    </td>
		</tr>
	</table>
	</div>
	</div>
	</div>
	</div>
	
<tags:packFormPrint></tags:packFormPrint>

	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<h3 class="box-title">运单信息</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse" type="button">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
	<div class="box-body">
	<!-- 运单信息 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<tr name="noexist">
		     <td colspan="10" height="50"  style="vertical-align:middle; text-align: center;color: gray;font-size: 20px;">无数据</td>
		</tr>
		
		<thead name="exist" style="display: none;">
			<th>运单编号</th>
			<th>快递公司</th>
			<th>省、市、区</th>
			<th>收货人</th>
			<th>联系手机</th>
			<th>联系座机</th>
			<th>收货人地址</th>
		</thead>
		
		<tr name="exist" style="display: none;">
		    <td id="expressNo"></td>
		    <td id="expressName"></td>
		    <td id="area"></td>
		    <td id="custna"></td>
		    <td id="mobile"></td>
		    <td id="teleph"></td>
		    <td id="address"></td>
		  </tr>
	</table>
	</div>
	</div>
	</div>
	</div>
	
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<h3 class="box-title">订单详细信息</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse" type="button">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
	<div class="box-body">
	<!-- 订单详细信息 -->
	<table class="table " style="margin-top: 0px;">
		<tr name="noexist">
		     <td colspan="10" height="50"  style="vertical-align:middle; text-align: center;color: gray;font-size: 20px;">无数据1</td>
		</tr>
		
		<thead name="exist" style="display: none;">
			<th>序号</th>
			<th>SKU</th>
			<th>商品名称</th>
			<th>商品数量</th>
			<th>需验货数量</th>
			<th>已验货数量</th>
		</thead>
		<tbody name="exist" id="orderItems"></tbody>
	</table>
	</div>
	</div>
	</div>
	</div>
</section>	
</body>
</html>
